package com.liuzhu.chaoshikucungunli.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.liuzhu.chaoshikucungunli.model.User;

public class UserDao {
    private DBHelper dbHelper;

    public UserDao(Context context) {
        dbHelper = new DBHelper(context);
    }

    // 根据用户名获取用户信息
    public User getUserByUsername(String username) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        User user = null;

        String selection = DBHelper.COLUMN_USERNAME + " = ?";
        String[] selectionArgs = {username};

        Cursor cursor = db.query(DBHelper.TABLE_USER, null, selection, selectionArgs, null, null, null);

        if (cursor.moveToFirst()) {
            user = new User();
            user.setId(cursor.getLong(cursor.getColumnIndex(DBHelper.COLUMN_ID)));
            user.setUsername(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_USERNAME)));
            user.setPassword(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_PASSWORD)));
            user.setPhone(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_PHONE)));
            user.setRole(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_ROLE)));
            user.setCompany(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_COMPANY)));
            user.setAvatar(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_AVATAR)));
            user.setNotification(cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_NOTIFICATION)) == 1);
        }

        cursor.close();
        db.close();
        return user;
    }

    // 添加新用户
    public long addUser(User user) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DBHelper.COLUMN_USERNAME, user.getUsername());
        values.put(DBHelper.COLUMN_PASSWORD, user.getPassword());
        values.put(DBHelper.COLUMN_PHONE, user.getPhone());
        values.put(DBHelper.COLUMN_ROLE, user.getRole());
        values.put(DBHelper.COLUMN_COMPANY, user.getCompany());
        values.put(DBHelper.COLUMN_AVATAR, user.getAvatar());
        values.put(DBHelper.COLUMN_NOTIFICATION, user.isNotification() ? 1 : 0);

        long id = db.insert(DBHelper.TABLE_USER, null, values);
        db.close();
        return id;
    }

    // 更新用户信息
    public int updateUser(User user) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DBHelper.COLUMN_PASSWORD, user.getPassword());
        values.put(DBHelper.COLUMN_PHONE, user.getPhone());
        values.put(DBHelper.COLUMN_ROLE, user.getRole());
        values.put(DBHelper.COLUMN_COMPANY, user.getCompany());
        values.put(DBHelper.COLUMN_AVATAR, user.getAvatar());
        values.put(DBHelper.COLUMN_NOTIFICATION, user.isNotification() ? 1 : 0);

        String whereClause = DBHelper.COLUMN_ID + " = ?";
        String[] whereArgs = {String.valueOf(user.getId())};

        int count = db.update(DBHelper.TABLE_USER, values, whereClause, whereArgs);
        db.close();
        return count;
    }

    // 更新用户密码
    public int updatePassword(long userId, String newPassword) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DBHelper.COLUMN_PASSWORD, newPassword);

        String whereClause = DBHelper.COLUMN_ID + " = ?";
        String[] whereArgs = {String.valueOf(userId)};

        int count = db.update(DBHelper.TABLE_USER, values, whereClause, whereArgs);
        db.close();
        return count;
    }

    // 更新通知设置
    public int updateNotification(long userId, boolean notification) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DBHelper.COLUMN_NOTIFICATION, notification ? 1 : 0);

        String whereClause = DBHelper.COLUMN_ID + " = ?";
        String[] whereArgs = {String.valueOf(userId)};

        int count = db.update(DBHelper.TABLE_USER, values, whereClause, whereArgs);
        db.close();
        return count;
    }

    // 删除用户
    public int deleteUser(long userId) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        String whereClause = DBHelper.COLUMN_ID + " = ?";
        String[] whereArgs = {String.valueOf(userId)};

        int count = db.delete(DBHelper.TABLE_USER, whereClause, whereArgs);
        db.close();
        return count;
    }
} 